//
//  HomeViewController.m
//  Chirismus
//
//  Created by wangning on 15/11/2.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "HomeViewController.h"
#import "CityChooseViewController.h"
#import "Define.h"
#import "PageView.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"city"] == nil) {
        [self setNavBar:@"北京"];
    }else{
    [self setNavBar:[[NSUserDefaults standardUserDefaults] valueForKey:@"city"]];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCity:) name:@"getCity" object:nil];
    
    
    _dataSource = @[
                    @{
                        @"imageName":@"大连_1.png",
                        @"title":@"大连按摩",
                        @"content":@"按摩介绍，阿斯顿发生发送到发送到发送到发送到发送到发送到发送到发送到法师法师的法师",
                        },
                    @{
                        @"imageName":@"大连_1.png",
                        @"title":@"大连按摩",
                        @"content":@"按摩介绍，阿斯顿发生发送到发送到发送到发送到发送到发送到发送到发送到法师法师的法师",
                        },
                    @{
                        @"imageName":@"大连_1.png",
                        @"title":@"大连按摩",
                        @"content":@"按摩介绍，阿斯顿发生发送到发送到发送到发送到发送到发送到发送到发送到法师法师的法师",
                        },
                    
                    ];
}
#pragma -mark 城市选择
- (void)getCity:(NSNotification *)noti{
    NSString *strCity = [noti object];
    [self setNavBar:strCity];
    [self.tableView reloadData];
}

- (void)setNavBar:(NSString *)city{
    UIButton *cityBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30*F_WIDTH, 30*F_HEIGHT)];
    [cityBtn setTitle:city forState:UIControlStateNormal];
    [cityBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cityBtn.titleLabel.font = [UIFont systemFontOfSize:12*F_WIDTH];
    
    [cityBtn addTarget:self action:@selector(cityChoose) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *cityBar = [[UIBarButtonItem alloc] initWithCustomView:cityBtn];
    self.navigationItem.leftBarButtonItem = cityBar;
}

- (void)cityChoose{
    [self.navigationController pushViewController:[[CityChooseViewController alloc] init] animated:YES];
}

#pragma -mark setCell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"identifier%ld",indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    NSDictionary *dic = [_dataSource objectAtIndex:indexPath.row];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15*F_WIDTH, 10*F_HEIGHT, 80*F_WIDTH, 80*F_HEIGHT)];
    imageView.image = [UIImage imageNamed:dic[@"imageName"]];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100*F_WIDTH, 10*F_HEIGHT, 200*F_WIDTH, 20*F_HEIGHT)];
    titleLabel.text = dic[@"title"];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100*F_WIDTH, 35*F_HEIGHT, 200*F_WIDTH, 65*F_HEIGHT)];
    contentLabel.text = dic[@"content"];
    contentLabel.numberOfLines = 0;
    
    [cell.contentView addSubview:imageView];
    [cell.contentView addSubview:titleLabel];
    [cell.contentView addSubview:contentLabel];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100*F_HEIGHT;
}

#pragma -mark setHeader
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150*F_HEIGHT)];
    //设定广告页 Begin
    NSArray *imageArray = [[NSArray alloc] initWithObjects:@"大连_1.png",@"大连_2.png",@"大连_3.png",nil];
    PageView *pageView = [[PageView alloc] initPageViewFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150*F_HEIGHT)];
    pageView.isWebImage = NO;
    pageView.imageArray = imageArray;
    pageView.duration = 3.0;
    
    [headerView addSubview:pageView];
    //设定广告页 End
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 150*F_HEIGHT;
}

- (void)didSelectPageViewWithNumber:(NSInteger)selectNumber{
    if (selectNumber == 0) {
        NSLog(@"进入第一个广告页面");
    }else if (selectNumber == 1){
        NSLog(@"进入第二个广告页面");
    }else if (selectNumber == 2){
        NSLog(@"进入第三个广告页面");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
